package com.vietnam.power.util;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.vietnam.power.constant.AppConfigurationConstant;
import com.vietnam.power.constant.GeneralConstant;
import com.vietnam.power.domain.Article;
import com.vietnam.power.domain.ArticleSearchCriteria;
import com.vietnam.power.domain.FileUpload;
import com.vietnam.power.domain.ProductGroup;
import com.vietnam.power.service.ArticleService;
import com.vietnam.power.service.FileService;
import com.vietnam.power.service.ProductService;

public class SpringInterceptor implements HandlerInterceptor {
	@Autowired
	private ProductService productService;

	@Autowired
	private FileService fileService;
	
	@Autowired
	private ArticleService articleService;

	private static Logger logger = Logger.getLogger(SpringInterceptor.class);
	
	private static int counter = 0;

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		logger.debug("Before process incomming request");

		return true;
	}

	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		logger.debug("After process incomming request");
		if (!request.getServletPath().contains(GeneralConstant.ADMIN_PATH)){
			List<ProductGroup> productGroups = productService.getProductGroupAsClientMenu();
			List<FileUpload> banners = fileService.getFilesByName(AppConfigurationConstant.FILE_UPLOAD.FILE_BANNER_NAME);
			List<FileUpload> footers = fileService.getFilesByName(AppConfigurationConstant.FILE_UPLOAD.FILE_FOOTER_NAME);
			List<FileUpload> sliders = fileService.getFilesByName(AppConfigurationConstant.FILE_UPLOAD.FILE_SLIDER_NAME);
			List<FileUpload> ads = fileService.getFilesByName(AppConfigurationConstant.FILE_UPLOAD.FILE_ADVERTISING_NAME);
			request.setAttribute("productGroup", productGroups);
			request.setAttribute("banners", banners);
			request.setAttribute("footers", footers);
			request.setAttribute("sliders", sliders);
			request.setAttribute("ads", ads);
		} 
		
		ArticleSearchCriteria asc = new ArticleSearchCriteria();
		asc.setArticleGroupName(AppConfigurationConstant.ARTICLE_GROUP.CONTACT);
		List<Article> artList = articleService.getArticleListByGroupName(asc, null);
		if (CollectionUtils.isNotEmpty(artList)) {
			request.setAttribute("footerContact", artList.get(0).getEmail());
		}
		
		//insert 
		Object counter = request.getSession().getAttribute("counter");
		if (counter != null) {
			counter = (Integer) counter;
			
			
		}
	}

	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		logger.debug("Finish process incomming request");
	}
}
